<?php
namespace Admin\Controller;

use Think\Controller;
use Think\Upload;

class BasicController extends Controller
{
    public function index()
    {
    }

    //颜色列表
    public function colorList()
    {
        $obj = M("pro_color");
        $count = $obj->count();
        $p = getPage($count);
        $list = $obj->limit($p->firstRow, $p->listRows)->select();
        $this->assign('datas', $list);
        $this->assign('page', $p->show());

        if (isMobile()) {
            $this->display("colorList_m");
        } else {
            $this->display();
        }
    }

    //新增颜色页面
    public function colorAdd()
    {
        if (isMobile()) {
            $this->display("colorAdd_m");
        } else {
            $this->display();
        }
    }

    //新增颜色
    public function colorAdd2()
    {
        $data["color"] = $_POST["color"];
        $data["type"] = $_POST["type"];
        $upload = new Upload();
        $upload->maxSize = 3145728;
        $upload->exts = array('jpg', 'png', 'jpeg');
        $upload->rootPath = "./Public/Color/";
        $upload->savePath = "";
        $upload->saveName = $data["color"];
        $upload->autoSub = false;
        $info = $upload->uploadOne($_FILES['pic']);
        $data["pic"] = $info["savename"];
        $timestamp = time();
        $data["updated"] = $timestamp;
        $obj = M("pro_color");
        $cid = $obj->add($data);
        if (!empty($cid)) {
            $this->success("添加成功", "colorList");
        } else {
            $this->error("添加失败", "colorList");
        }
    }

    //删除颜色
    public function colorDel($id)
    {
        $obj = M("pro_color");
        $data = $obj->where("cid=" . $id)->delete();
        if ($data == false || $data == 0) {
            $this->error('删除失败', '../../colorList');
        } else {
            $this->success('删除成功', '../../colorList');
        }
    }

    //生成菜单
    public function menu()
    {
        $menu = M('menu');
        $updated = time();

        $datas = [
            //一级菜单
            ["mid" => 1, "menu" => "首页", "parent" => 0, "updated" => $updated],
            ["mid" => 2, "menu" => "订单管理", "parent" => 0, "updated" => $updated],
            ["mid" => 3, "menu" => "用户管理", "parent" => 0, "updated" => $updated],
            ["mid" => 4, "menu" => "权限管理", "parent" => 0, "updated" => $updated],
            ["mid" => 5, "menu" => "参数管理", "parent" => 0, "updated" => $updated],
            ["mid" => 6, "menu" => "财务管理", "parent" => 0, "updated" => $updated],
            ["mid" => 7, "menu" => "统计管理", "parent" => 0, "updated" => $updated],

            //二级菜单
            ["mid" => 10, "menu" => "已预约", "parent" => 2, "updated" => $updated],
            ["mid" => 11, "menu" => "已派单", "parent" => 2, "updated" => $updated],
            ["mid" => 12, "menu" => "已量衣", "parent" => 2, "updated" => $updated],
            ["mid" => 13, "menu" => "已完成", "parent" => 2, "updated" => $updated],
            ["mid" => 14, "menu" => "客户列表", "parent" => 3, "updated" => $updated],
            ["mid" => 15, "menu" => "员工列表", "parent" => 3, "updated" => $updated],
            ["mid" => 16, "menu" => "创建账号", "parent" => 4, "updated" => $updated],
            ["mid" => 17, "menu" => "权限分配", "parent" => 4, "updated" => $updated],
            ["mid" => 18, "menu" => "面料管理", "parent" => 5, "updated" => $updated],
            ["mid" => 19, "menu" => "工厂", "parent" => 6, "updated" => $updated],
            ["mid" => 20, "menu" => "量体", "parent" => 6, "updated" => $updated],
            ["mid" => 21, "menu" => "收款", "parent" => 6, "updated" => $updated],
            ["mid" => 22, "menu" => "销售", "parent" => 6, "updated" => $updated],
            ["mid" => 23, "menu" => "返工记录", "parent" => 7, "updated" => $updated],

            //三级菜单
            ["mid" => 100, "menu" => "待分配", "parent" => 12, "updated" => $updated],
            ["mid" => 101, "menu" => "已分配", "parent" => 12, "updated" => $updated],
            ["mid" => 102, "menu" => "工厂完工", "parent" => 13, "updated" => $updated],
            ["mid" => 103, "menu" => "待配送", "parent" => 13, "updated" => $updated],
            ["mid" => 104, "menu" => "已配送", "parent" => 13, "updated" => $updated],
            ["mid" => 105, "menu" => "返工单", "parent" => 13, "updated" => $updated]
        ];
        $rs = $menu->addAll($datas);
        if ($rs) {
            $desc = '成功';

        } else {
            $desc = '失败';
        }
        $this->assign('desc', $desc);
        $this->display();
    }

    //生成权限
    public function permission()
    {
        $menu = M('permiss');
        $updated = time();

        $datas = [
            //首页
            ["permiss" => "已预约数量", "updated" => $updated],
            ["permiss" => "已派单数量", "updated" => $updated],
            ["permiss" => "已量衣数量", "updated" => $updated],
            ["permiss" => "已完成数量", "updated" => $updated],
            ["permiss" => "待配送数量", "updated" => $updated],
            ["permiss" => "已配送数量", "updated" => $updated],
            ["permiss" => "返工单数量", "updated" => $updated],
            ["permiss" => "订单搜索", "updated" => $updated],

            /*
            订单管理
            */

            //已预约
            ["permiss" => "新增预约记录", "updated" => $updated],
            ["permiss" => "搜索预约单", "updated" => $updated],
            ["permiss" => "编辑预约单", "updated" => $updated],
            ["permiss" => "派单", "updated" => $updated],
            ["permiss" => "删除预约单", "updated" => $updated],
            //已派单
            ["permiss" => "新增客户", "updated" => $updated],
            ["permiss" => "录入订单", "updated" => $updated],
            ["permiss" => "修改预约单", "updated" => $updated],
            //已量衣
            ["permiss" => "批量分配工厂", "updated" => $updated],
            ["permiss" => "分配工厂", "updated" => $updated],
            ["permiss" => "查看订单详情", "updated" => $updated],
            ["permiss" => "删除订单", "updated" => $updated],
            //已完成
            ["permiss" => "批量配送", "updated" => $updated],
            ["permiss" => "配送", "updated" => $updated],
            ["permiss" => "批量打印", "updated" => $updated],
            ["permiss" => "批量完成配送", "updated" => $updated],
            ["permiss" => "批量分配配送", "updated" => $updated],
            ["permiss" => "完成配送", "updated" => $updated],
            ["permiss" => "打印", "updated" => $updated],
            ["permiss" => "分配配送", "updated" => $updated],
            ["permiss" => "返工", "updated" => $updated],
            ["permiss" => "导出excel", "updated" => $updated],
            ["permiss" => "导出excel", "updated" => $updated],
            ["permiss" => "修改返工单", "updated" => $updated],
            /*
             * 用户管理
             * */
            ["permiss" => "新增客户", "updated" => $updated],
            ["permiss" => "下单", "updated" => $updated],
            ["permiss" => "编辑客户信息", "updated" => $updated],
            ["permiss" => "查看订单信息", "updated" => $updated],
            ["permiss" => "删除客户", "updated" => $updated],
            ["permiss" => "删除员工", "updated" => $updated],
            /*
             * 权限管理
             * */
        ];
        $rs = $menu->addAll($datas);
        if ($rs) {
            $desc = '成功';

        } else {
            $desc = '失败';
        }
        $this->assign('desc', $desc);
        $this->display("menu");
    }

    /*
     * 角色和菜单的关系，每个角色有哪些菜单
     * */
    public function menuRoleRel()
    {
        $obj = M("menu_role_rel");
        $role = M("user_role");
        $menu = M("menu");

        $roleRes = $role->select();
        $menuRes = $menu->select();
        foreach ($roleRes as $rr) {
            if ($rr['rid'] == 0) {//角色是admin的菜单
                foreach ($menuRes as $mr) {
                    $data = [
                        "rid" => 0,
                        "mid" => $mr['mid']
                    ];
                    $obj->add($data);
                }
            } elseif ($rr['rid'] == 2) {//角色是商家的菜单
                $nameArr = ['首页','订单管理','用户管理'];
                $data = [
                    "rid" => 2,
                    "mid" => $mr['mid']
                ];
                $obj->add($data);
            } elseif ($rr['rid'] == 3) {//role is factory's menu
                foreach ($menuRes as $mr) {
                    $data = [
                        "rid" => 0,
                        "mid" => $mr['mid']
                    ];
                    $obj->add($data);
                }
            }
        }
    }

    /*
     * 分配权限
     * */
    public function perAllot(){
        $menu = M("menu");
        $role = M("user_role");
       $firstArr = $menu->where("parent=0")->select();//一级菜单
        $menuArr = [];
        foreach($firstArr as $f){
            $secondArr = $menu->where("parent=".$f["mid"])->select();//二级菜单

            $thirds = [];
            foreach($secondArr as $se){
                $thirdArr = $menu->where("parent=".$se["mid"])->select();//三级菜单
                $third["th"]=$thirdArr;
                $thirds[] = $third;

            }
            $ziArr["first"] = $f;
            $ziArr["second"] = $secondArr;
            $ziArr["third"] = $thirds;
            $menuArr[] = $ziArr;
        }

        //role
        $roles = $role->select();

        $datas = $menuArr;
        $this->assign("datas",$datas);
        $this->assign("roles",$roles);
        $this->display();
    }

    /*
     * 分配权限，提交表单
     * @wxj
     * */
    public function perPost(){
        $data = I("post.");
        $returnData['status'] = 1;
        $returnData['content'] = json_encode($data);
        $this->ajaxReturn($returnData, 'json');
    }
}